package com.treetao.union.web.controller.mall;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.treetao.union.db.model.MallTopicSku;
import com.treetao.union.db.service.MallPSkuService;
import com.treetao.union.db.service.MallTopicSkuService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.treetao.union.db.model.vo.LayerData;
import com.treetao.union.db.model.vo.RestResponse;
import com.treetao.union.web.constant.Constants;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import java.util.Map;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 专题关联商品  前端控制器
 * </p>
 *
 * @author Dev.M
 * @since 2020-04-15
 */
@Controller
@RequestMapping(Constants.AMIND_PREFIX + "/mall-topic-sku")
public class MallTopicSkuController {

    @Autowired
    private MallTopicSkuService mallTopicSkuService;
    @Autowired
    private MallPSkuService mallPSkuService;

    @GetMapping("list")
    public String list(HttpServletRequest req,Model model){
    	Map<String, String[]> params = req.getParameterMap();
    	for(String key : params.keySet()) {
    		model.addAttribute(key,params.get(key)==null?null:params.get(key)[0]);
    	}
        return "/admin/mall-topic-sku/list";
    }

    @PostMapping("list")
    @ResponseBody
    public LayerData<MallTopicSku> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                      ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<MallTopicSku> layerData = new LayerData<>();
        QueryWrapper<MallTopicSku> wrapper = new QueryWrapper<MallTopicSku>();
        if(!map.isEmpty()){
        	String delFlag = (String) map.get("delFlag");
            if(StringUtils.isNotBlank(delFlag)) {
                wrapper.eq("del_flag",Boolean.valueOf(delFlag));
            }else{
                map.remove("del_flag");
            }
            String topicId = (String) map.get("topicId");
            if(StringUtils.isNotBlank(topicId)) {
                wrapper.eq("topic_id",topicId);
            }else{
                map.remove("topicId");
            }
        }
        IPage<MallTopicSku> pageData = mallTopicSkuService.page(new Page<>(page,limit),wrapper);
        if(pageData.getRecords()!=null) {
        	for(MallTopicSku topicSku : pageData.getRecords()) {
        		topicSku.setSku(mallPSkuService.getById(topicSku.getSkuId()));
        	}
        }
        layerData.setData(pageData.getRecords());
        layerData.setCount(pageData.getTotal());
        return layerData;
    }

    @GetMapping("add")
    public String add(HttpServletRequest req,Model model){
    	Map<String, String[]> params = req.getParameterMap();
    	for(String key : params.keySet()) {
    		model.addAttribute(key,params.get(key)==null?null:params.get(key)[0]);
    	}
        return "/admin/mall-topic-sku/add";
    }

    @PostMapping("add")
    @ResponseBody
    public RestResponse add(MallTopicSku mallTopicSku){
    	String skuIds = mallTopicSku.getSkuIds();
    	if(StringUtils.isNotEmpty(skuIds)) {
    		List<MallTopicSku> list = new ArrayList<MallTopicSku>();
    		String[] arr = skuIds.split(",");
    		for(String skuId : arr) {
    			MallTopicSku sk = new MallTopicSku();
    			sk.setTopicId(mallTopicSku.getTopicId());
    			sk.setSkuId(Long.valueOf(skuId));
    			list.add(sk);
    		}
    		mallTopicSkuService.saveBatch(list);
    		return RestResponse.success();
    	}
        return RestResponse.failure("请选择商品");
    }
    
    @PostMapping("batchDel")
    @ResponseBody
    public RestResponse batchDel(@RequestParam(value = "ids[]",required = false)List<Long> ids){
        if(null == ids || ids.size()==0){
            return RestResponse.failure("请选择一项");
        }
        return RestResponse.success().setDataOnlyResult( mallTopicSkuService.removeByIds(ids));
    }

    @PostMapping("delete")
    @ResponseBody
    public RestResponse delete(@RequestParam(value = "id",required = false)Long id){
        if(null == id || 0 == id){
            return RestResponse.failure("ID不能为空");
        }
        return RestResponse.success().setDataOnlyResult(mallTopicSkuService.removeById(id));
    }
    
    @PostMapping("change-status")
    @ResponseBody
    public RestResponse change_status(@RequestParam(value = "id",required = true)Long id,@RequestParam(value = "status",required = false)Boolean status){
       MallTopicSku mallTopicSku = mallTopicSkuService.getById(id);
        mallTopicSku.setDelFlag(status);
        mallTopicSkuService.updateById(mallTopicSku);
        return RestResponse.success();
    }

}